package cn.com.css.taxInspection.workflow.service.impl;

import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.entity.AjGzddRequest;
import cn.com.css.taxInspection.workflow.entity.GzddAjContentEntity;
import cn.com.css.taxInspection.workflow.entity.WorkflowLogEntity;
import cn.com.css.taxInspection.workflow.mapper.GzddAjContentMapper;
import cn.com.css.taxInspection.workflow.service.GzddAjContentService;
import cn.com.css.taxInspection.workflow.service.WorkflowLogService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("GzddAjContentService")
public class GzddAjContentServiceImpl extends ServiceImpl<GzddAjContentMapper, GzddAjContentEntity> implements GzddAjContentService {


    @Resource
    GzddAjContentService gzddAjContentService;

    @Resource
    private WorkflowLogService workflowLogService;

    @Resource
    private GzddAjContentMapper gzddAjContentMapper;

    @Override
    public GzddAjContentEntity queryGzddAjContentByJcajxxuuid(String jcajxxuuid, String id) {
        LambdaQueryWrapper<GzddAjContentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GzddAjContentEntity::getDduuid, id);
        queryWrapper.eq(GzddAjContentEntity::getJcajxxuuid, jcajxxuuid);
        List<GzddAjContentEntity> gzddAjContentEntityList = gzddAjContentService.list(queryWrapper);
        return gzddAjContentEntityList.get(0);
    }


    @Override
    @DS("oracle")
    public void tbGzdd(AjGzddRequest ajGzddRequest) {
        //更新调度内容
        GzddAjContentEntity gzddAjContentEntity = new GzddAjContentEntity();
        gzddAjContentEntity.setUuid(ajGzddRequest.getAjContentId());
        gzddAjContentEntity.setDdzt("已填报");
        gzddAjContentEntity.setAjcontent(ajGzddRequest.getAjContent());
        gzddAjContentService.updateById(gzddAjContentEntity);
        //判断调度内容是否都填写完毕
        judgeGzddContent(ajGzddRequest.getDduuid());
    }

    //判断调度内容是否都填写完毕
    public void judgeGzddContent(String dduuid) {
        LambdaQueryWrapper<GzddAjContentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GzddAjContentEntity::getDduuid, dduuid);
        List<GzddAjContentEntity> gzddAjContentEntityList = gzddAjContentService.list(queryWrapper);
        // 检查是否所有 ajContent 都不为 null  如果所有 ajContent 都不为 null，返回 true，否则返回 false
        boolean result = gzddAjContentEntityList.stream().allMatch(entity -> TsBqUtils.isNotNull(entity.getAjcontent()));
        // 如果是true，则调整改调度任务完成状态
        if (result) {
            LambdaQueryWrapper<WorkflowLogEntity> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.eq(WorkflowLogEntity::getId, dduuid);
            WorkflowLogEntity workflowLogEntity = workflowLogService.getOne(queryWrapper1);
            workflowLogEntity.setType(Constants.GZDD_DD_FINISH);
            workflowLogEntity.setUpdateTime(TsBqUtils.getSystemTime());
            workflowLogService.updateById(workflowLogEntity);
        }
    }


    @Override
    public GzddAjContentEntity queryGzddAjContentByJcajxxuuid(String jcajxxuuid) {
        LambdaQueryWrapper<GzddAjContentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GzddAjContentEntity::getJcajxxuuid, jcajxxuuid);
        List<GzddAjContentEntity> gzddAjContentEntityList = gzddAjContentService.list(queryWrapper);
        return gzddAjContentEntityList.get(0);
    }

    @Override
    public void deleteDataById(String id) {
        gzddAjContentMapper.deleteDataById(id);

    }
}